<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="服务标题中文" field="name_zh">
            <a-input v-model="searchForm.name_zh" placeholder="请输入服务标题中文" />
          </a-form-item>
        </a-col>
      
      </template>
	  <template #sort_order="{ record }">
	    <a-input-number
	        v-model="record.sort_order"
	        :min="0"
	        style="width: 80px"
	        @change="changeSortOrder(record.jump_config_id, record.sort_order)"
	    />
	  </template>

      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../api/config'

// 引用定义
const crudRef = ref()
const editRef = ref()

// 搜索表单
const searchForm = ref({
  name_zh: ''
})


// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  pk: 'jump_config_id',
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/JumpConfig/JumpConfig/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/JumpConfig/JumpConfig/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/JumpConfig/JumpConfig/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/JumpConfig/JumpConfig/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/JumpConfig/JumpConfig/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
 {
   title: '排序',
   dataIndex: 'sort_order',
   width: 150,
   slots: { customRender: 'sort_order' } // 新增插槽配置
 },
  { title: '服务图标', dataIndex: 'icon', type: 'image', width: 180 },
  { title: '服务标题中文', dataIndex: 'name_zh', width: 180 },
  { title: '服务标题英文', dataIndex: 'name_en', width: 180 },
  { title: '服务标题日语', dataIndex: 'name_ja', width: 180 },
  { title: '服务标题韩文', dataIndex: 'name_ko', width: 180 },
  { title: '小程序app_id', dataIndex: 'appid', width: 180 },
  { title: '小程序跳转链接', dataIndex: 'path', width: 180 },
])
//jia
const changeSortOrder = async (id,sortOrder) => {
  try {
    const response = await api.updateSortOrder({
      id: id,
      sort_order: sortOrder
    });
    if (response.code === 200) {
      Message.success('排序修改成功');
      crudRef.value?.refresh();
    } else {
      Message.error('排序修改失败');
    }
  } catch (error) {
    console.log(error,"error")
    Message.error('排序修改失败1');
  }
}

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
